﻿using System.Security.Cryptography;
using System.Text;

namespace RSTAi.Platform.Test.Common
{
    /// <summary>
    /// 订单
    /// </summary>
    public class UniqueData
    {
      
        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns></returns>
        public static int Number(int Length)
        {
            int randomNum = new Random().Next(1000, 9999);
            return randomNum;
        }
        /// <summary>
        /// 生成随机字母字符串(数字字母混和)
        /// </summary>
        /// <param name="codeCount">待生成的位数</param>
        public static string GetCheckCode(int codeCount)
        {
            string str = string.Empty;
            int rep = 0;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                else
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                str = str + ch.ToString();
            }
            return str;
        }
        /// <summary>
        /// 根据日期和随机码生成订单号
        /// </summary>
        /// <returns></returns>
        public static string GetOrderNumber()
        {
            //string num = DateTime.Now.ToString("yyMMddHHmmss");//格式化日期yyyyMMddHHmmssffff
            //string input = num + Number(4).ToString();
            //string str = string.Empty;
            //byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            //using (SHA1 sha1 = SHA1.Create())
            //{
            //    byte[] hashBytes = sha1.ComputeHash(inputBytes);
            //    str = BitConverter.ToString(hashBytes).Replace("-", "");
            //}
            int _sn = 0;
            if (_sn == int.MaxValue)
            {
                _sn = 0;
            }
            else
            {
                _sn++;
            }

            Thread.Sleep(100);
            string str = DateTime.Now.Ticks.ToString();

            return str;
        }
    }
}
